package com.yc.springmvc.controller;

import com.yc.springmvc.MyDispatchServlet;
import com.yc.springmvc.bean.User;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.print.attribute.standard.Media;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("controller")
@Controller
public class IndexController {

    //produces = MediaType.APPLICATION_JSON_VALUE + "charset=utf-8"  设置返回的字符串转为  json格式
    //设置字符集为utf-8  (对于很多老的服务器，这可能是ISO-8859-1)
    @RequestMapping(path = "hello")
    @ResponseBody
    public String hello(@RequestBody User user){
        System.out.println("user = " + user);
        return "张三";
    }

    @RequestMapping("abc")
    public String A(String page) {
        page = "".equals(page) ? "A" : page;
        return "redirect:../" + page + ".html";
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MyDispatchServlet myDispatchServlet = new MyDispatchServlet();
        myDispatchServlet.service(req,resp);
        return "hello";
    }
}
